/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DialogGeracaoJava.java
 *
 * Created on 27/09/2008, 16:21:08
 */

package sintelo.visao;

import java.io.File;
import javax.swing.JFileChooser;
import sintelo.gerador.GeradorReconhecedores;
import sintelo.gerador.java.GeradorExemploJava;
import sintelo.gerador.java.GeradorLexicoJava;
import sintelo.gerador.java.GeradorSintaticoJava;

/**
 *
 * @author phireis
 */
public class TelaGeracaoJava extends javax.swing.JDialog {
    
    private GeradorReconhecedores gerador;

    /** Creates new form DialogGeracaoJava */
    public TelaGeracaoJava(java.awt.Frame parent, GeradorReconhecedores gerador) {
        super(parent);
        this.gerador = gerador;
        initComponents();
    }

    public void desabilitaSintatico(){
        checkSintatico.setSelected(false);
        checkExemplo.setSelected(false);
        checkSintatico.setEnabled(false);
        checkExemplo.setEnabled(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtLocal = new javax.swing.JTextField();
        btnSelecionar = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        txtPacote = new javax.swing.JTextField();
        jPanel5 = new javax.swing.JPanel();
        checkLexico = new javax.swing.JCheckBox();
        checkSintatico = new javax.swing.JCheckBox();
        checkExemplo = new javax.swing.JCheckBox();
        jPanel1 = new javax.swing.JPanel();
        btnGerar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Geração de código Java");
        setModal(true);

        jPanel2.setLayout(new java.awt.GridLayout(4, 1));

        jLabel1.setText("Local: ");
        jPanel3.add(jLabel1);

        txtLocal.setMinimumSize(new java.awt.Dimension(200, 19));
        txtLocal.setPreferredSize(new java.awt.Dimension(200, 19));
        jPanel3.add(txtLocal);

        btnSelecionar.setText("Selecionar");
        btnSelecionar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSelecionarActionPerformed(evt);
            }
        });
        jPanel3.add(btnSelecionar);

        jPanel2.add(jPanel3);

        jLabel3.setText("Pacote");
        jPanel4.add(jLabel3);

        txtPacote.setMinimumSize(new java.awt.Dimension(200, 19));
        txtPacote.setPreferredSize(new java.awt.Dimension(200, 19));
        jPanel4.add(txtPacote);

        jPanel2.add(jPanel4);

        checkLexico.setSelected(true);
        checkLexico.setText("Léxico");
        jPanel5.add(checkLexico);

        checkSintatico.setSelected(true);
        checkSintatico.setText("Sintático");
        jPanel5.add(checkSintatico);

        checkExemplo.setSelected(true);
        checkExemplo.setText("Exemplo");
        jPanel5.add(checkExemplo);

        jPanel2.add(jPanel5);

        btnGerar.setText("Gerar");
        btnGerar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGerarActionPerformed(evt);
            }
        });
        jPanel1.add(btnGerar);

        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });
        jPanel1.add(btnCancelar);

        jPanel2.add(jPanel1);

        getContentPane().add(jPanel2, java.awt.BorderLayout.CENTER);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-441)/2, (screenSize.height-176)/2, 441, 176);
    }// </editor-fold>//GEN-END:initComponents

    private void btnSelecionarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSelecionarActionPerformed
        JFileChooser dir = new JFileChooser();
        dir.setAcceptAllFileFilterUsed(false);
        dir.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

        dir.showOpenDialog(this);

        File file = dir.getSelectedFile();
        if(file == null){
            return;
        }

        txtLocal.setText(file.toString());


    }//GEN-LAST:event_btnSelecionarActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void btnGerarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGerarActionPerformed
        String local = txtLocal.getText();
        String pacote = txtPacote.getText();

        boolean blex = checkLexico.isSelected();
        boolean bsin = checkSintatico.isSelected();
        boolean bexp = checkExemplo.isSelected();

        try{
            if(blex){
                GeradorLexicoJava glex = new GeradorLexicoJava(gerador);
                glex.gera(local, pacote);
            }

            if(bsin){
                GeradorSintaticoJava gsin = new GeradorSintaticoJava(gerador);
                gsin.gera(local, pacote);
            }

            if(bexp){
                GeradorExemploJava gexp = new GeradorExemploJava();
                gexp.gera(local, pacote);
            }

            dispose();
        } catch(Exception e){
            Mensagens.alerta(this, e.getMessage(), "Geração de reconhecedores");
        }

    }//GEN-LAST:event_btnGerarActionPerformed



    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnGerar;
    private javax.swing.JButton btnSelecionar;
    private javax.swing.JCheckBox checkExemplo;
    private javax.swing.JCheckBox checkLexico;
    private javax.swing.JCheckBox checkSintatico;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JTextField txtLocal;
    private javax.swing.JTextField txtPacote;
    // End of variables declaration//GEN-END:variables

}
